﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using MvcEngine.Core.Validation;

namespace MvcEngine.Mvc.UI
{
    public static class ValidationExtensions
    {
        public static ValidationView Validators(this HtmlHelper helper, string formName)
        {
            return new ValidationView(new ObjectValidatorBuilder(formName));
        }

        public static ValidationView Validators<TModel>(this HtmlHelper helper, string formName)
            where TModel : class
        {
            ValidationView view = new ValidationView(new ObjectValidatorBuilder(formName));

            var properties = typeof(TModel).GetProperties();
            foreach (var property in properties)
            {
                var validators = property.GetCustomAttributes(false).Where(v => v is FieldValidatorAttribute).Cast<FieldValidatorAttribute>();
                if (!validators.Any(v => v.EnableClientValidation))
                    continue;

                view.Items(item => { item.For(property.Name).AddValidators(validators.Where(v => v.EnableClientValidation)); });
            }
            return view;
        }
    }
}
